package com.xxl.job.executor.service.thread;

import com.ruoyi.bizsys.domain.CreditAccount;
import com.ruoyi.business.service.IBLoanInfoService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
public class DailyCutBatchJobThread implements Callable {

    private String threadName;

    private Date batchDate;

    private String redisName;

    private RedisCache redisCache;

    private IBLoanInfoService loanInfoService;

    public DailyCutBatchJobThread(String threadName, Date batchDate, String redisName, IBLoanInfoService loanInfoService, RedisCache redisCache) {
        super();
        this.threadName = threadName;
        this.batchDate = batchDate;
        this.redisName = redisName;
        this.redisCache = redisCache;
        this.loanInfoService = loanInfoService;
    }

    @Override
    public String toString() {
        return "DailyCutBatchJobThread [threadName=" + threadName + ", batchDate=" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, this.batchDate) + ", redisName=" + redisName + "]";
    }

    @Override
    public Object call() {
        String result = ":success";
        log.info(this.threadName + "=======[START]=======" + this);

        List<CreditAccount> accountList = redisCache.getCacheList(this.redisName);
        log.info("当前线程有 {} 个客户需要执行", accountList.size());

        for (CreditAccount account : accountList) {
            log.info("客户号 {} 开始执行跑批操作", account.getCustNo());

            //执行日切任务
            loanInfoService.doBatchJobByCust(batchDate, account);

            log.info("客户号{}执行跑批操作正常结束", account.getCustNo());
        }

        redisCache.deleteObject(this.redisName);

        log.info(this.threadName + "=======[END]===run result:" + result);
        return threadName + result;
    }
}